<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="律师所名称" prop="lawFirmName">
        <el-input
          v-model="queryParams.lawFirmName"
          placeholder="请输入律师所名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in flow_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发布时间" style="width: 308px">
        <el-date-picker
                v-model="daterangePublishTime"
                size="small"
                value-format="YYYY-MM-DD HH:mm:ss"
                type="datetimerange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['test:mater:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['test:mater:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['test:mater:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['test:mater:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="materList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50" align="center">
        <template #default="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="律师所名称" align="center" prop="lawFirmName" :show-overflow-tooltip="true"/>
      <el-table-column label="地址" align="center" prop="address" :show-overflow-tooltip="true"/>
      <el-table-column label="logo图片id" align="center" prop="fileId" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.fileId" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="state" :show-overflow-tooltip="true">
        <template #default="scope">
          <dict-tag :options="flow_status" :value="scope.row.state"/>
        </template>
      </el-table-column>
        <el-table-column label="发布时间" align="center" prop="publishTime" width="180">
            <template #default="scope">
                <span>{{ parseTime(scope.row.publishTime) }}</span>
            </template>
        </el-table-column>
      <el-table-column label="创建者" align="center" prop="createBy" :show-overflow-tooltip="true"/>
        <el-table-column label="创建时间/提交时间" align="center" prop="createTime" width="180">
            <template #default="scope">
                <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
        </el-table-column>
      <el-table-column label="操作" align="center" width="110" class-name="small-padding fixed-width" fixed="right">
        <template #default="scope">
          <el-tooltip content="详情" placement="top">
            <el-button link type="primary" icon="View" @click="handleDetail(scope.row)" v-hasPermi="['test:mater:detail']"></el-button>
          </el-tooltip>
          <el-tooltip content="编辑" placement="top">
            <el-button
                link
                type="primary"
                icon="Edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['test:mater:edit']"
            ></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button
                link
                type="primary"
                icon="Delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['test:mater:remove']"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <Dialog ref="dialog" @refresh="getList"></Dialog>
  </div>
</template>

<script setup name="Mater">
import { listMater, delMater } from "@/api/test/mater";
import Dialog from "@/views/test/mater/dialog";

const { proxy } = getCurrentInstance();
const { flow_status } = proxy.useDict('flow_status');

const materList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const daterangePublishTime = ref([]);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    lawFirmName: null,
    address: null,
    state: null,
    publishTime: null,
  },
});

const { queryParams } = toRefs(data);

/** 查询主子演示列表 */
function getList() {
  loading.value = true;
  queryParams.value.paramsStr = "";
  if (null != daterangePublishTime && '' != daterangePublishTime) {
    queryParams.value.paramsStr =
        JSON.stringify({
          "beginPublishTime": daterangePublishTime.value[0],
          "endPublishTime": daterangePublishTime.value[1]
        });
  }
  listMater(queryParams.value).then(response => {
    materList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangePublishTime.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  proxy.$refs["dialog"].show();
}

/** 详情按钮操作 */
function handleDetail(row) {
  proxy.$refs["dialog"].show(row.id, true);
}

/** 修改按钮操作 */
function handleUpdate(row) {
  const _id = row.id || ids.value
  proxy.$refs["dialog"].show(_id);
}

	
/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除主子演示编号为"' + _ids + '"的数据项？').then(function() {
    loading.value = true;
    return delMater(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
    loading.value = false;
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('test/mater/export', {
    ...queryParams.value
  }, `主子演示_${new Date().getTime()}.xlsx`)
}

getList();
</script>
